<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>一个简单的购物车的例子</title>
  <script src="../vue.js"></script>
</head>
<body>
  <div id="app">
    <div>
      <h2>我的购物车</h2>
      <ul>
        <li v-for="item in cartItems">
          {{item.name}}：￥{{item.price}} x {{item.quantity}} = ￥{{ item | singlePrice }}
        </li>
      </ul>
      <p>Total: ￥{{totalPrice}}</p>
      <button @click="clearCart">Clear Cart</button>
    </div>
  
  </div>
  <script>
    new Vue({
      el: '#app',
      data: {
        cartItems: [
          { name: 'Product A', price: 10, quantity: 2 },
          { name: 'Product B', price: 20, quantity: 1 },
          { name: 'Product C', price: 30, quantity: 3 }
        ]
      },
      computed: {
        totalPrice() {
          return this.cartItems.reduce((summary, item) => {
            summary += item.price * item.quantity
            return summary
          }, 0)
        }
      },
      filters: {
        singlePrice(value) {
          return value.price * value.quantity
        }
      },
      methods: {
        clearCart() {
          this.cartItems = []
        }
      },
      watch: {
        // cartItems(newValue) {
        //   console.log(newValue)
        // },
        cartItems: {
          handler(newValue) {
            console.log(newValue)
          },
          immediate: true
        }
      },
    })
  </script>
</body>
</html>